package com.workcenter.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.workcenter.entity.Sys.SysUserDO;
import com.workcenter.entity.Sys.UserRoleDO;
import com.workcenter.mapper.UserMapper;
import com.workcenter.mapper.UserRoleMapper;
import com.workcenter.service.UserService;

@Service
@Transactional(value = "transactionManager", rollbackFor = Exception.class)
public class UserServiceImp implements UserService {
  @Autowired
  UserMapper userMapper;
  @Autowired
  UserRoleMapper userRoleMapper;
  private static final Logger logger = LoggerFactory.getLogger(UserService.class);

  @Override
  public SysUserDO get(Long id) {
    List<Long> roleIds = userRoleMapper.listRoleId(id);
    SysUserDO user = userMapper.get(id);
    user.setroleIds(roleIds);
    return user;
  }

  @Override
  public List<SysUserDO> list(Map<String, Object> map) {
    return userMapper.list(map);
  }

  @Override
  public int count(Map<String, Object> map) {
    return userMapper.count(map);
  }

  @Transactional
  @Override
  public int save(SysUserDO user) {
    int count = userMapper.save(user);
    Long userId = user.getUserId();
    List<Long> roles = user.getroleIds();
    userRoleMapper.removeByUserId(userId);
    List<UserRoleDO> list = new ArrayList<>();
    for (Long roleId : roles) {
      UserRoleDO ur = new UserRoleDO();
      ur.setUserId(userId);
      ur.setRoleId(roleId);
      list.add(ur);
    }
    if (list.size() > 0) {
      userRoleMapper.batchSave(list);
    }
    return count;
  }

  @Override
  public int update(SysUserDO user) {
    int r = userMapper.update(user);
    Long userId = user.getUserId();
    List<Long> roles = user.getroleIds();
    userRoleMapper.removeByUserId(userId);
    List<UserRoleDO> list = new ArrayList<>();
    for (Long roleId : roles) {
      UserRoleDO ur = new UserRoleDO();
      ur.setUserId(userId);
      ur.setRoleId(roleId);
      list.add(ur);
    }
    if (list.size() > 0) {
      userRoleMapper.batchSave(list);
    }
    return r;
  }

  @Override
  public int remove(Long userId) {
    userRoleMapper.removeByUserId(userId);
    return userMapper.remove(userId);
  }

  @Override
  public boolean exit(Map<String, Object> params) {
    boolean exit;
    exit = userMapper.list(params).size() > 0;
    return exit;
  }

  @Override
  public Set<String> listRoles(Long userId) {
    return null;
  }

  @Override
  public int resetPwd(SysUserDO user) {
    int r = userMapper.update(user);
    return r;
  }

  @Transactional
  @Override
  public int batchremove(List<Long> userIds) {
    int count = userMapper.batchRemove(userIds);
    userRoleMapper.batchRemoveByUserId(userIds);
    return count;
  }

}
